package current.security.user.domain;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import org.apache.commons.lang.StringUtils;
import org.hibernate.annotations.GenericGenerator;

@Entity(name="Resc")
@Table(name="TBL_RESC")
public class Resc {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	@Id
	@GenericGenerator(name="idGenerator", strategy="uuid") //这个是hibernate的注解
	@GeneratedValue(generator="idGenerator") //使用uuid的生成策略
	private String id;
	
	//资源名称
	private String name;
	//资源中文名称（显示用）
	private String cname;
	//资源字符串，目前系统用这个字段进行的匹配
	private String string;
	//描述
	private String memo;
	//类型
	private String type;
	
	private String tree;
	 //mappedBy 说明User不负责维护关系
    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY,mappedBy="resc")
	private Set<R_Resc> roles;
	public String getName() {
		return name;
	}
	
	public String getRoleAuthorities() {
    	String sroles = "";
    	List<String> roleAuthorities = new ArrayList<String>();
    	if( roles != null && roles.size() > 0 ){
	    	for(R_Resc role : roles) {
	    		roleAuthorities.add(role.getRole().getName());
	    	}
    		sroles = StringUtils.join(roleAuthorities.iterator(), ",");
    	}else{
    		sroles = "NOROLE";
    	}
        return sroles + ":" + this.getType();
    }
	
	public void setName(String name) {
		this.name = name;
	}
	
	public String getString() {
		return string;
	}

	public void setString(String string) {
		this.string = string;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getCname() {
		return cname;
	}

	public void setCname(String cname) {
		this.cname = cname;
	}

	public String getMemo() {
		return memo;
	}

	public void setMemo(String memo) {
		this.memo = memo;
	}
	public Set<R_Resc> getRoles() {
		return roles;
	}

	public void setRoles(Set<R_Resc> roles) {
		this.roles = roles;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getTree() {
		return tree;
	}

	public void setTree(String tree) {
		this.tree = tree;
	}
}
